#!/usr/bin/perl
# adds header properties
# by andy

use strict;
use warnings;

use DBI;
use List::Util qw(sum);

use Data::Dumper;

# Skip DB for now...
#       # Database parameters - change these as appropriate.
#       $username = '';$password = '';$database = '';$hostname = '';
#       $tablename = "chains" # change this too
#       $dbh = DBI->connect("dbi:mysql:database=$database;" .
#          "host=$hostname;port=3306", $username, $password);

open(LIST, "<nrp.txt")
        or die "ERROR: Could not open nonredundant_pdb.txt\n";

print "ALTER TABLE helidb_tbl ADD organism VARCHAR(80);\n";

sub yr {

        my $in = shift;

        my $year;
        my $month;
               
        if (length($in) == 2) {
                # do the best we can; assume that 80 means 80s
                if ($in > 80) {
                        $year = "19" . $in;
                } else {
                        $year = "20" . $in;
                }
        } elsif (length == 4) {
                $year = $in;
        }
       
        return $year;
}

sub mon {

        my $month;
       
        # convert to caps
        $_[0] = uc($_[0]);
       
        if ($_[0] eq 'JAN') {
                $month = '01';
        } elsif ($_[0] eq 'FEB') {
                $month = '02';
        } elsif ($_[0] eq 'MAR') {
                $month = '03';
        } elsif ($_[0] eq 'APR') {
                $month = '04';
        } elsif ($_[0] eq 'MAY') {
                $month = '05';
        } elsif ($_[0] eq 'JUN') {
                $month = '06';
        } elsif ($_[0] eq 'JUL') {
                $month = '07';
        } elsif ($_[0] eq 'AUG') {
                $month = '08';
        } elsif ($_[0] eq 'SEP') {
                $month = '09';
        } elsif ($_[0] eq 'OCT') {
                $month = '10';
        } elsif ($_[0] eq 'NOV') {
                $month = '11';
        } elsif ($_[0] eq 'DEC') {
                $month = '12';
        } elsif (length($_[0]) == 2) {
                $month = $_[0];
        }
       
        return $month;
}

foreach my $pdb_code (<LIST>) {

        chomp $pdb_code;
       
        my $pdb_file = "pdb_files/" . $pdb_code . ".pdb";
       
        #print STDERR $pdb_code . " ----------------------------------------\n";
       
        open (PDB, "<$pdb_file")
                or die "ERROR: Could not open PDB file $pdb_file.\n";
       
        my $title;
        my $resolution;
        my $date;
        my $organism;
   
        foreach (<PDB>) {
		if (defined $title and defined $resolution and defined $date and defined $organism) {
 			last;
		}
    		if (/ORGANISM_SCIENTIFIC/) {
        		$organism = $_;
			$organism =~ s/SOURCE\s+[1-9]+\s+ORGANISM_SCIENTIFIC:*\s*//;
			$organism =~ s/;//;
			$organism =~ s/\s+$//;
			chomp $organism;
    		}
    		if (/ORGANISM_COMMON/) {
        		$organism = $_;
			$organism =~ s/SOURCE\s+[1-9]+\s+ORGANISM_COMMON:*\s*//;
			$organism =~ s/;//;
			$organism =~ s/\s+$//;
			chomp $organism;
    		}
               
    		if (/^TITLE/) {
                        if (!defined $title) {
                                $title = substr $_, 10;
                        } else {
                                $title .= substr $_, 10;
                        }
                        $title =~ s/\s*$//;
			#$title =~ s/\"/\\\"/;
			#$title =~ s/\'/\\\'/;
			#$title =~ s/\*/\\\*/;
                }
               
                if (/^REMARK   2 RESOLUTION./) {
                        if (!defined $resolution || $resolution ne 'NMR') {
                                $resolution = substr $_, 26;
                                $resolution =~ s/\s*$//;
                        }
                }
               
                if (/^EXPDTA/) {
                        if (/NMR/) {
                                $resolution = "NMR";
                        }
                }
               
                if (/^HEADER/) {
                        $date = substr $_, 50, 9;
                        $date =~ s/\s*$//;
                }
        }
       

        # $date probably looks like dd-MMM-yy
        my @datefields = split '-', $date;
        $date = yr($datefields[2]) . '-' . mon($datefields[1]) . '-' . $datefields[0];
        if (!defined($organism) or $organism eq ' ') {
    		$organism = 'unknown';
  	}
	$resolution =~ s/NGSTOMS\.//;
        print "UPDATE helidb_tbl SET resolution='$resolution', title='$title', date_added='$date', organism='$organism' WHERE pdbcode='$pdb_code';\n";
       
}

close (LIST);

exit 0;
